package com.insurance.service.impl;

import java.util.List;

import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.insurance.bean.Agent;
import com.insurance.service.AgentService;


@Transactional
public class AgentServiceBean implements AgentService {
	@Resource private SessionFactory sessionFactory;

	public void save(Agent agent){
		sessionFactory.getCurrentSession().persist(agent);
	}
	
	public void update(Agent agent){
		sessionFactory.getCurrentSession().merge(agent);
	}
	
	public void censor(Integer agentid){
		Agent agent = (Agent)sessionFactory.getCurrentSession().get(Agent.class, agentid);
		agent.setCheckout(1);
		sessionFactory.getCurrentSession().merge(agent);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Agent getAgent(Integer agentid){
		return (Agent)sessionFactory.getCurrentSession().get(Agent.class, agentid);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Agent getAgentByUserName(String userName){
		return (Agent) sessionFactory.getCurrentSession().
		createQuery("from Agent a where a.userName="+userName).list();
	}
	
	public void delete(Integer agentid){
		sessionFactory.getCurrentSession().delete(
				sessionFactory.getCurrentSession().load(Agent.class, agentid));
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	
	public List<Agent> getAgents(){		
		return sessionFactory.getCurrentSession().createQuery("from Agent").list();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	
	public List<Agent> getAgentsChecked(){
		return sessionFactory.getCurrentSession().
		createQuery("from Agent where checkout='1'").list();
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	
	public List<Agent> getAgentsUnChecked(){
		return sessionFactory.getCurrentSession().
		createQuery("from Agent where checkout='0'").list();
	}
	
	public Agent verifyAgent(Agent agent) {
		String hql = "from Agent a where a.userName = ? and a.password = ?";
		try {
			@SuppressWarnings("rawtypes")
			List list = sessionFactory.getCurrentSession().createQuery(hql).
			setParameter(0, agent.getUserName()).setParameter(1,agent.getPassword()).list();
			if(list != null && list.size() > 0){
				return (Agent)list.get(0);
			} else{
				return null;
			}
			
		} catch(Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}
	
	public String verifyUserName(String userName) {
		String hql = "from Agent a where a.userName ="+"userName";
		try {
			@SuppressWarnings("rawtypes")
			List list = sessionFactory.getCurrentSession().createQuery(hql).list();
			if(list != null && list.size() > 0){
				return "true";
			} else{
				return "false";
			}
			
		} catch(Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}
		

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Agent> searchAgent(Agent agent){
		String hql = "from Agent a where a.area = ? and a.companyName = ? and a.checkout='1' ";
		return sessionFactory.getCurrentSession().createQuery(hql).
		setParameter(0, agent.getArea()).setParameter(1,agent.getCompanyName()).list();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Agent> getAgentsCheckedTop10(){
		return sessionFactory.getCurrentSession().
		createQuery("from Agent where checkout='1' order by id desc").
		setFirstResult(0).setMaxResults(10).list();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Agent> getAgentsCheckedTop5(){
		return sessionFactory.getCurrentSession().
		createQuery("from Agent where checkout='1' order by id desc").
		setFirstResult(0).setMaxResults(5).list();
	}
}
